<template>
    <h2>用户借款列表</h2>
    借款名称<input type="text" v-model="name">
    利息 <select v-model="iid">
        <option value="0">请选择</option>
            <option v-for="i in bin" :value="i.Iid">{{i.Ill}}</option>
            
    </select>
    
    <button @click="Show" class="btn">查询</button>
    <button @click="Show" class="btn">升序</button>
    <button @click="Show1" class="btn">降序</button>
    
    <hr>
    <table class="table table-bordered">
        <thead>
            <tr>
                <th>借款ID</th>
                <th>借款名称</th>
                <th>借款最高金额</th>
                <th>借款年限</th>
                <th>利息</th>
                <th>借款简介</th>
                <th>借款发布时间</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            <tr v-for="i in info">
                <th>{{i.Bid}}</th>
                <th>{{i.Bname}}</th>
                <th>{{i.BmaxPrice}}</th>
                <th>{{i.Byear}}</th>
                <th>{{i.Ill}}</th>
                <th>{{i.Binfo}}</th>
                <th>{{i.Btime}}</th>
                <th>
                    <a href="#" @click="jump(i.Bid)">借款</a>
                </th>
            </tr>
        </tbody>
    </table>
    当前是第{{ page.pageIndex }}页，每页显示{{ page.pageSize }}条数据，总共{{ page.listCount }}条数据
    <hr>
    <a href="#" @click="Pages(1)">首页</a>
    <a href="#" @click="Pages(2)">上一页</a>
    <a href="#" @click="Pages(3)">下一页</a>
    <a href="#" @click="Pages(4)">末页</a>
    
    <input type="text" v-model="tz">
    <button  @click="Pagest()">跳转</button>
    
    <hr>
</template>
<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import axios from 'axios';
import { useStore } from '../../store/index.ts';


const store = useStore();
const router = useRouter();
const route = useRoute();

const info = ref({
    Bid:0,
    Bname:'',
    BmaxPrice:0,
    Byear:0,
    Iid:0,
    Binfo:'',
    Btime:'',
    Ill:'',
});

const page = ref({
    pageSize:2,
    pageIndex:1,
    listCount:0,
    pageCount:0,
})
const name = ref('');
const iid = ref(0);

const bin = ref({
    Iid:0,
    Ill:'',
})

onMounted(() => {
    bind();
    Show();
})

const bind = ()=>{
    axios({
        method: 'get',
        url: 'http://localhost:61498/api/Borrow/BindInvest',
        
    })
       .then(res => {
            console.log(res.data);
            bin.value= res.data;
        })
        .catch(err => {
            console.log(err);
        })
}



const Show = () => {
    axios({
        method: 'get',
        url: 'http://localhost:61498/api/Borrow/ShowBorrowInfo',
        params: {
            name:name.value,
            iid:iid.value,
            pageSize:page.value.pageSize,
            pageIndex:page.value.pageIndex,
        }
    }).then(res => {
        info.value = res.data.Plist;
        page.value.listCount = res.data.listCount;
        page.value.pageCount = res.data.pageCount;
        console.log(res.data);
    })
       
        .catch(err => {
            console.log(err);
        })
}

const Show1 = () => {
    axios({
        method: 'get',
        url: 'http://localhost:61498/api/Borrow/ShowBorrowInfo1',
        params: {
            name:name.value,
            iid:iid.value,
            pageSize:page.value.pageSize,
            pageIndex:page.value.pageIndex,
        }
    }).then(res => {
        info.value = res.data.Plist;
        page.value.listCount = res.data.listCount;
        page.value.pageCount = res.data.pageCount;
        console.log(res.data);
    })
       
        .catch(err => {
            console.log(err);
        })
}

const Pages = (msg:number) => {
    switch(msg){
        case 1:
            page.value.pageIndex=1;
            break;
        case 2:
            page.value.pageIndex--;
            if(page.value.pageIndex<1){
                page.value.pageIndex=1;
                alert("已经是第一页了")
            }
            break;
        case 3:
            page.value.pageIndex++;
            if(page.value.pageIndex>page.value.pageCount){
                page.value.pageIndex=page.value.pageCount;
                alert("已经是最后一页了")
            }
            break;
        case 4:
            page.value.pageIndex=page.value.pageCount;
            break;
    }
        
    Show();
}

const tz = ref('');
const Pagest = () => {
    if(tz.value>page.value.pageCount){
        alert("超出范围了");
        page.value.pageIndex=page.value.pageCount;
    }else if(tz.value<1){
        alert("超出范围了");
        page.value.pageIndex=1;
    }else{
        page.value.pageIndex=tz.value;
    }
    Show();
}

const jump = (Bid:number) => {
    router.push({
        path: '/UserAddBorroe',
        query: {
            
            Bid:Bid,
        }
    })
}




</script>